package google.entry.worker;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2016/12/25.
 */
public class AcgnWorker extends Thread {

    private String[] args;
    private File tasks;
    private File output;

    public AcgnWorker(String[] args, File tasks, File output) {
        setName("AcgnWorker");
        this.args = args;
        this.tasks = tasks;
        this.output = output;
    }

    public void work(String[] args, File tasks, File output) throws IOException {
        List<String> list = FileUtils.readLines(tasks, "UTF-8");
        String url = "http://www.acgn.ren/search?q=[COMPANY]&safe=strict&tbs=cdr:1,cd_min:[START],cd_max:[END]&tbm=nws";
        FileWriter fileWriter = new FileWriter(output, true);
        Set<String> set = new HashSet<>();
        List<String> lines = FileUtils.readLines(output, "UTF-8");
        for (String str : lines) {
            String[] arr = str.split("\t");
            set.add(arr[0] + "\t" + arr[1]);
        }
        for (String str : list) {
            String roundUrl = url.replace("[COMPANY]", URLEncoder.encode(str));
            for (int i = 2004; i < 2016; i++) {
                String start = i + "/1/1";
                String end = i + "/12/31";
                if (set.contains(str + "\t" + start + "-" + end))
                    continue;
                String urlStr = roundUrl.replace("[START]", start).replace("[END]", end);
                System.out.println(getName() + " crawls : " + urlStr);
                String content = get(urlStr);
                Pattern pattern = Pattern.compile("id=\"resultStats\">(.+?)<nobr>", Pattern.DOTALL);
                Matcher matcher = pattern.matcher(content);
                if (matcher.find()) {
                    String string = matcher.group(1);
                    string = string.replaceAll("[\\u4e00-\\u9fa5]", "").replaceAll(",", "").trim();
                    if (i == 2004 && !string.trim().equals("0"))
                        break;
                    fileWriter.write(str + "\t" + start + "-" + end + "\t" + string + "\n");
                    fileWriter.flush();
                } else if (content.contains("未搜到与")) {
                    fileWriter.write(str + "\t" + start + "-" + end + "\t" + 0 + "\n");
                    fileWriter.flush();
                } else {
                    System.err.println(getName() + " " + urlStr);
                    this.interrupt();
                }
            }
        }
        fileWriter.flush();
        fileWriter.close();
    }

    private String get(String url) {
        String content = "";
        try {
            sleep(1000);
            Document document = getDocument(url);
            content = document.html();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return content;
    }

    @Override
    public void run() {
        try {
            work(args, tasks, output);
        } catch (IOException e) {
            try {
                sleep(5000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            try {
                work(args, tasks, output);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    Document getDocument(String url) {
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(150000).validateTLSCertificates(false).userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36").get();
        } catch (Exception ex) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getDocument(url);
        }
        if (document == null) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getDocument(url);
        }
        return document;
    }
}
